<template>
  <a-spin :spinning="spinning">
    <page-header-wrapper>
      <applicationSearch
        :filterFields="['SubmitToCity']"
        :moreHandles="[{ name: '批量上报', data: selectedRowKeys }]"
        @search="handleSearch"
        @reset="handleReset"
      ></applicationSearch>
      <a-card :bordered="false">
        <s-table
          ref="table"
          size="default"
          rowKey="ID"
          :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          :columns="columns"
          :data="loadData"
          :scroll="{ x: true }"
          :pagination="{ showTotal: (total) => `共 ${total} 条` }"
        >
          <div slot="tabAction">
            <buttonSelect
              class="m-l-10"
              v-model="typeSelect"
              :options="[
                { value: '', label: '全部' },
                { value: '0', label: '未上报' },
                { value: '-1', label: '已退回' },
              ]"
              @change="handleButtonSelectChange"
            ></buttonSelect>
          </div>
          <span slot="index" slot-scope="text, record, index">{{ index + 1 }}</span>
          <span slot="VerifyDate" slot-scope="text">{{ text | moment('YYYY-MM-DD') }}</span>
          <span slot="description" slot-scope="text">
            <ellipsis :length="30">{{ text }}</ellipsis>
          </span>
          <div slot="action" slot-scope="text, record">
            <y-button type="link" @click="handleReport(record)">上报</y-button>
          </div>
        </s-table>
      </a-card>
    </page-header-wrapper>
  </a-spin>
</template>

<script>
import { STable, Ellipsis, buttonSelect, applicationSearch } from '@/components'
import { getProposerList, postProposerExamine } from '@/api/common'
import { postProposerExamineBatch } from '@/api/DistrictReviewReportManagement'
import moment from 'moment'
export default {
  name: 'cityComplexInquireList',
  components: {
    STable,
    Ellipsis,
    buttonSelect,
    applicationSearch,
  },
  computed: {
    textColor() {
      return function (text) {
        const classMap = new Map([
          ['已退回', 'text-error'],
          ['未上报', 'text-warning'],
          ['符合', 'text-success'],
          ['不符合', 'text-error'],
          ['未审核', 'text-warning'],
          ['未审批', 'text-warning'],
        ])
        return classMap.get(text)
      }
    },
  },
  provide() {
    return {
      provideData: this.data,
    }
  },
  data() {
    return {
      typeSelect: '',
      spinning: false, // 加载遮罩
      queryParam: {
        UserID: this.$store.getters.userid,
        Mode: 'DistrictReport',
        strContent: {},
      },
      columns: [
        {
          title: '序号',
          align: 'center',
          width: 80,
          scopedSlots: { customRender: 'index' },
        },
        {
          title: '申请编号',
          align: 'center',
          dataIndex: 'ApplyNo',
          width: 140,
        },
        {
          title: '申请人',
          align: 'center',
          dataIndex: 'Proposer',
          width: 140,
        },
        {
          title: '身份证号',
          align: 'center',
          dataIndex: 'IDCard',
          width: 180,
        },
        {
          title: '社区/街道',
          align: 'center',
          dataIndex: 'TownshipName',
          width: 240,
          sorter: true,
          scopedSlots: { customRender: 'description' },
        },
        {
          title: '区级审核结果',
          align: 'center',
          dataIndex: 'VerifyResult',
          customRender: (text) => {
            let value = '未审核'
            if (!this.$isEmpty(text)) {
              value = text
            }
            return <span class={this.textColor(value)}>{value}</span>
          },
          width: 180,
        },
        {
          title: '审核日期',
          align: 'center',
          dataIndex: 'VerifyDate',
          scopedSlots: { customRender: 'VerifyDate' },
          width: 140,
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'SubmitToCity',
          fixed: 'right',
          customRender: (value) => {
            const text = value === '-1' ? '已退回' : '未上报'
            return <span class={this.textColor(text)}>{text}</span>
          },
          width: 140,
        },
        {
          title: '操作',
          width: 140,
          key: 'action',
          align: 'center',
          fixed: 'right',
          scopedSlots: { customRender: 'action' },
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        requestParameters.strContent.SubmitToCity = this.typeSelect
        return getProposerList(requestParameters).then((res) => {
          return res
        })
      },
      selectedRowKeys: [],
    }
  },
  methods: {
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    handleSearch(values) {
      this.queryParam.strContent = values
      this.$refs.table.refresh(true)
    },
    handleReset() {
      this.queryParam.strContent = {}
      this.typeSelect = ''
      this.$refs.table.refresh(true)
    },
    handleButtonSelectChange(e) {
      this.$refs.table.refresh(true)
    },
    handleBatchReport() {
      if (this.$isEmpty(this.selectedRowKeys)) {
        this.$message.warning('至少选择一条用户信息')
      } else {
        this.$confirm({
          title: '提示',
          content: `确定上报所选的${this.selectedRowKeys.length}条用户信息吗？`,
          onOk: () => {
            return postProposerExamineBatch({
              State: 'DistrictReport',
              ProposerModules: {
                ID: this.selectedRowKeys.join(','),
                SubmitToUser: this.$store.getters.nickname,
                SubmitToDate: moment().format('YYYY-MM-DD HH:mm:ss'),
                LastAlterDate: moment().format('YYYY-MM-DD HH:mm:ss'),
                LastAlterUserID: this.$store.getters.userid,
              },
            }).then((res) => {
              this.$message.success('上报成功')
              this.$refs.table.refresh()
              this.selectedRowKeys = []
            })
          },
        })
      }
    },
    handleReport(record) {
      this.$confirm({
        title: '提示',
        content: '确定上报该用户的信息吗？',
        onOk: () => {
          return postProposerExamine({
            State: 'DistrictReport',
            ProposerModules: {
              ID: record.ID,
              SubmitToUser: this.$store.getters.nickname,
              SubmitToDate: moment().format('YYYY-MM-DD HH:mm:ss'),
              LastAlterDate: moment().format('YYYY-MM-DD HH:mm:ss'),
              LastAlterUserID: this.$store.getters.userid,
            },
          }).then((res) => {
            this.$message.success('上报成功')
            this.$refs.table.refresh()
            this.selectedRowKeys = []
          })
        },
      })
    },
  },
}
</script>
<style lang="less" scoped></style>
